package LeetCode.LeetCode2;

/**
 * 在排序数组中查找元素的第一个和最后一个位置
 * @author lumin
 * @date 2022/03/31 16:16
 **/
public class Num34 {
    //双指针
    public int[] searchRange(int[] nums, int target) {
        int[] arr = {-1,-1};
        if(nums.length == 0){
            return arr;
        }
        if(nums.length == 1 && nums[0] == target){
            arr[0] = 0;
            arr[1] = 0;
            return arr;
        }
        if(nums.length == 1 && nums[0] != target){
            return arr;
        }
        int left = 0;
        int right = nums.length - 1;
        while(left <= right){
            if(arr[0] < 0){
                if(nums[left] == target){
                    arr[0] = left;
                }
            }
            if(arr[1] < 0){
                if(nums[right] == target){
                    arr[1] = right;
                }
            }
            if(nums[left] != target){
                left++;
            }
            if(nums[right] != target){
                right--;
            }
            if(arr[0] != -1 && arr[1] != -1){
                return arr;
            }
        }
        return arr;
    }
}